package com.cswnode.demo.service.impl;

import com.cswnode.demo.dao.ProfessionalTypeDao;
import com.cswnode.demo.entity.ProfessionalType;
import com.cswnode.demo.enums.YesOrNoEnum;
import com.cswnode.demo.service.ProfessionalTypeService;
import com.cswnode.demo.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 专业类型(ProfessionalType)表服务实现类
 *
 * @author makejava
 * @since 2021-03-09 16:17:43
 */
@Service("professionalTypeService")
public class ProfessionalTypeServiceImpl implements ProfessionalTypeService {
    @Resource
    private ProfessionalTypeDao professionalTypeDao;
    /**
     * 事物
     */
    @Autowired
    private DataSourceTransactionManager transactionManager;

    /**
     * 通过ID查询单条数据
     *
     * @param professionalTypeId 主键
     * @return 实例对象
     */
    @Override
    public ProfessionalType queryById(String professionalTypeId) {
        return this.professionalTypeDao.queryById(professionalTypeId);
    }


    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ProfessionalType> queryAllByLimit(int offset, int limit, String professionalTypeName) {
        return this.professionalTypeDao.queryAllByLimit(offset, limit,professionalTypeName);
    }

    /**
     * 新增数据
     *
     * @param professionalType 实例对象
     * @return 实例对象
     */
    @Override
    public ProfessionalType insert(ProfessionalType professionalType) {
        professionalType.setProfessionalTypeId(Utils.GetUUID());
        professionalType.setProfessions(null);
        professionalType.setCreateTime(new Date());
        professionalType.setIsDelete(YesOrNoEnum.NO.getCode());
        this.professionalTypeDao.insert(professionalType);
        return professionalType;
    }

    /**
     * 修改数据
     *
     * @param professionalType 实例对象
     * @return 实例对象
     */
    @Override
    public ProfessionalType update(ProfessionalType professionalType) {
        this.professionalTypeDao.update(professionalType);
        return this.queryById(professionalType.getProfessionalTypeId());
    }

    /**
     * 通过主键删除数据
     *
     * @param professionalTypeId 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(String professionalTypeId) {
        if (StringUtils.isEmpty(professionalTypeId)){
            return false;
        }
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setName("SomeTxName");
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);
        try{
            professionalTypeDao.deleteById(professionalTypeId);
            professionalTypeDao.deleteByPro(professionalTypeId);
        }catch (Exception e){
            transactionManager.rollback(status);
            throw e;
        }
        return true;
    }

    @Override
    public int queryNum(String professionalTypeName) {
        return this.professionalTypeDao.queryNum(professionalTypeName);
    }

    @Override
    public List<ProfessionalType> query() {
        return this.professionalTypeDao.query();
    }
}